package IdManagement;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

public class UserAsClient extends User{
	private SortedMap<Instant,String> loginInfo=new TreeMap<>();
	private Instant now_date;
	private String addr;
	
	public UserAsClient(String id) {
		super(id);
	}

	public void init() throws IOException, SQLException {
		loadLoginInfo();
		loadFriendList();
	}
	
	protected void loadLoginInfo() throws IOException, SQLException {
		ResultSet rs=load(id,"_login_info");
		
		while(rs.next()) {
			String login_date=rs.getString(1);
			String login_address=rs.getString(2);
			Instant login_date_=Instant.ofEpochSecond(Long.parseLong(login_date));
			loginInfo.put(login_date_, login_address);
		}
		
		DBUtil.close();	
	}
	
	public SortedMap<Instant,String> getLoginInfo() {
		return loginInfo;
	}
	
	public void createLoginInfo(String addr) {
		this.now_date=Instant.now();
		this.addr=addr;
	}
	
	private void updateLoginInfo() throws SQLException, IOException {
		PreparedStatement pState=DBUtil.getConnection()
				.prepareStatement("insert into "+id+"_login_info value (?,?)");
		
		for (Entry<Instant,String> info: loginInfo.entrySet()) {
			String date=Long.toString(info.getKey().getEpochSecond());
			pState.setString(1, date);
			pState.setString(2, info.getValue());
			pState.executeUpdate();
		}
		
		DBUtil.close();
	}
	
	public void update(String ps,List<User> params) throws SQLException, IOException {
		PreparedStatement pState=DBUtil.getConnection().prepareStatement(ps);
		
	}
	
	public static void main(String...args) {
		
	}

}
